package com.example.shouYePoji.siYunShiPinTongJi;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 。
 * @version 1.0
 * Create by 2022/11/13 15:34
 * 计算上周的两个首尾日期
 */
public class CalculateLastWeekDate {
    //获取上周一
        public static Date geLastWeekMonday1(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, -7);
        return cal.getTime();
    }
    //获取上上周一
    public static Date geLastWeekMonday01(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, -14);
        return cal.getTime();
    }
    //得到上周一日期
    public static String szy(){
        Date g = new Date();
        Long timeStamp = g.getTime();
        SimpleDateFormat c=new SimpleDateFormat("yyy-MM-dd");
        String DateTime01 = c.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(DateTime01);
            return sdf.format(geLastWeekMonday1(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "出错了";
    }
    //得到上周一日期
    public static String sszy(){
        Date g = new Date();
        Long timeStamp = g.getTime();
        SimpleDateFormat c=new SimpleDateFormat("yyy-MM-dd");
        String DateTime01 = c.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(DateTime01);
            return sdf.format(geLastWeekMonday01(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "出错了";
    }
    //获取上周日
    public static Date geLastWeekMonday2(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }
    //获取上上周日
    public static Date geLastWeekMonday02(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, -8);
        return cal.getTime();
    }
    //得到上周日日期
    public static String szr(){
        Date g = new Date();
        Long timeStamp = g.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String DateTime01 = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        try {
            Date date = sdf.parse(DateTime01);
            return sdf.format(geLastWeekMonday2(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "出错了";
    }
    //得到上周日日期
    public static String sszr(){
        Date g = new Date();
        Long timeStamp = g.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String DateTime01 = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        try {
            Date date = sdf.parse(DateTime01);
            return sdf.format(geLastWeekMonday02(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "出错了";
    }
    public static void jkads(){
            Date g = new Date();
        Long timeStamp = g.getTime();
        SimpleDateFormat c=new SimpleDateFormat("yyy-MM-dd");
        String DateTime01 = c.format(new Date(Long.parseLong(String.valueOf(timeStamp))));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(DateTime01);
            System.out.println("今天是" + sdf.format(date));
            System.out.println("上周一" + sdf.format(geLastWeekMonday1(date)));
            System.out.println("本周一" + sdf.format(getThisWeekMonday(date)));
            System.out.println("下周一" + sdf.format(getNextWeekMonday(date)));
            System.out.println("上周日"+sdf.format(geLastWeekMonday2(date)));
        } catch (Exception e) {
            e.printStackTrace();
        }



    }
    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    public static Date getNextWeekMonday(Date date) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, 7);
        return cal.getTime();
    }
}
